package com.hy.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hy.admin.pojo.Lideshow;
import com.hy.admin.pojo.vo.LideshowVo;
import com.hy.file.service.FileStorageService;
import com.hy.mapper.LideshowMapper;
import com.hy.service.LideshowService;
import com.hy.util.common.Result;
import com.hy.util.common.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author Lenovo
 * @description 针对表【lideshow(轮播图)】的数据库操作Service实现
 * @createDate 2025-03-05 14:16:25
 */
@Service
public class LideshowServiceImpl extends ServiceImpl<LideshowMapper, Lideshow> implements LideshowService {
    @Autowired
    private LideshowMapper lideshowMapper;

    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 轮播图分页查询
     *
     * @param lideshowVo
     * @return
     */
    @Override
    public Result getLideshow(LideshowVo lideshowVo) {
        PageHelper.startPage(lideshowVo.getPageNum(), lideshowVo.getPageSize());
        List<Lideshow> lideshowList = lideshowMapper.getLideshow(lideshowVo);
        PageInfo<Lideshow> lideshowPageInfo = new PageInfo<>(lideshowList);
        return Result.build(lideshowPageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 添加轮播图
     * @param lideshow
     * @return
     */
    @Override
    public Result addLideshow(Lideshow lideshow) {
        lideshowMapper.insert(lideshow);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 上传图片
     * @param file
     * @return
     * @throws Exception
     */
    @Override
    public String updatefile(MultipartFile file) throws Exception {
        if(file==null || file.getSize()<0){
            return "";
        }
        String s = fileStorageService.uploadImageFile(file.getOriginalFilename(), file.getInputStream());
        return s;

    }


    /**
     *  删除轮播图
     * @param lideshow
     * @return
     */
    @Override
    public Result delete(Lideshow lideshow) {
        if(lideshow.getId()!=null){
           lideshowMapper.deleteById(lideshow);
            return Result.build(null, ResultCodeEnum.SUCCESS);
        }
        return Result.build(null, ResultCodeEnum.DATA_ERROR);
    }
}




